package com.hwy.widget.float_window

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.provider.Settings
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.hwy.widget.R
import com.hwy.widget.utils.LogUtil

class FloatWindowActivity : AppCompatActivity(), FloatWindowLayout.OnDismissListener {

    companion object {
        private const val TAG = "FloatWindowActivity"

        /**
         * 打开悬浮窗权限
         */
        private const val REQUEST_OPEN_OVERLAY = 100

    }

    private var simpleWindow: FloatWindowLayout? = null

    private var floatLogLayout: FloatLogLayout? = null

    private val handler by lazy {
        Handler(Looper.getMainLooper())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_float_window)

    }

    fun openSimpleWindow(view: View) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Log.d(TAG, "未获取到悬浮窗权限")
                startActivityForResult(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION).apply {
                    data = Uri.parse("package:${packageName}")
                }, REQUEST_OPEN_OVERLAY)
            } else {
                Log.d(TAG, "获取到悬浮窗权限")
                showSimpleWindow()
            }

        }
    }

    private fun showSimpleWindow() {
        Log.d(TAG, "showSimpleWindow")
        if (simpleWindow == null) {
            simpleWindow = LayoutInflater.from(this)
                .inflate(
                    R.layout.layout_float_window_wrapper,
                    window.decorView as ViewGroup,
                    false
                ) as? FloatWindowLayout
            simpleWindow?.onDismissListener = this
        }

        floatLogLayout =
            LayoutInflater.from(this).inflate(R.layout.layout_float_http, null) as? FloatLogLayout
        LogUtil.addLogListener(floatLogLayout)

        simpleWindow?.run {
            setTitle("请求日志")
            setContentView(floatLogLayout)
            show()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            REQUEST_OPEN_OVERLAY -> {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (!Settings.canDrawOverlays(this)) {
                        Log.d(TAG, "onActivityResult: 悬浮窗功能未启用")
                    } else {
                        // 某些机型可能存在页面回来权限还未授权成功，这里加一个延时
                        handler.postDelayed({
                            showSimpleWindow()
                        }, 1000)
                    }
                }
            }
        }
    }

    override fun onDismiss(layout: FloatWindowLayout) {
        simpleWindow = null
        LogUtil.removeLogListener(floatLogLayout)
    }

    fun createLog(view: View) {
        LogUtil.d("这是一条随机日志${System.currentTimeMillis()}")
    }

}